package Query.SearchEngine;
import java.util.Vector;
import Database.Document;
import Query.*;
import com.yahoo.search.*;
import java.io.IOException;

public class YahooEngineAdapter implements IEngineAdapter {
    
    public YahooEngineAdapter() {}
    
    public Vector<Document> Query(String query, int length)
    {
        Vector<Document> dresult = new Vector<Document>();        
        SearchClient client = new SearchClient(SearchEnginesKeys.YAHOO_KEY);
        WebSearchRequest request = new WebSearchRequest(query);

        try {
            WebSearchResults results = client.webSearch(request);
            for (int i = 0; i < results.listResults().length && length>0; i++, length--) {
                WebSearchResult result = results.listResults()[i];
                dresult.add(new Document(result.getUrl().trim(), result.getTitle(), result.getSummary(), this.getSearchEngine()));
            }
            return dresult;
        }
        catch (IOException e) {
            e.printStackTrace(System.err);
            return null;
        }
        catch (SearchException e) {
            e.printStackTrace(System.err);
            return null;
        }
    }
    
    public SearchEnginesTypes getSearchEngine()
    {
        return SearchEnginesTypes.YAHOO;
    }
}
